package com.ruoyi.project.wms.essentials.storage.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.wms.essentials.details.domain.WmsPurchaseDetails;
import com.ruoyi.project.wms.essentials.details.mapper.WmsPurchaseDetailsMapper;
import com.ruoyi.project.wms.essentials.purchase.domain.WmsPurchase;
import com.ruoyi.project.wms.essentials.purchase.mapper.WmsPurchaseMapper;
import com.ruoyi.project.wms.essentials.storage.domain.WmsStorage;
import com.ruoyi.project.wms.essentials.storage.mapper.WmsStorageMapper;
import com.ruoyi.project.wms.essentials.storage.service.IWmsStorageService;
import com.ruoyi.project.wms.space.domain.WmsStroeSpace;
import com.ruoyi.project.wms.space.mapper.WmsStroeSpaceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.text.Convert;

/**
 * 入库记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-29
 */
@Service
public class WmsStorageServiceImpl implements IWmsStorageService
{
    @Autowired
    private WmsStorageMapper wmsStorageMapper;

    @Autowired
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private WmsStroeSpaceMapper wmsStroeSpaceMapper;

    @Autowired
    private WmsPurchaseDetailsMapper wmsPurchaseDetailsMapper;

    /**
     * 查询入库记录
     * 
     * @param stoId 入库记录ID
     * @return 入库记录
     */
    @Override
    public WmsStorage selectWmsStorageById(Long stoId)
    {
        return wmsStorageMapper.selectWmsStorageById(stoId);
    }

    /**
     * 查询入库记录列表
     * 
     * @param wmsStorage 入库记录
     * @return 入库记录
     */
    @Override
    public List<WmsStorage> selectWmsStorageList(WmsStorage wmsStorage)
    {
        return wmsStorageMapper.selectWmsStorageList(wmsStorage);
    }

    /**
     * 新增入库记录
     * 
     * @param wmsStorage 入库记录
     * @return 结果
     */
    @Override
    public int insertWmsStorage(WmsStorage wmsStorage)
    {
        return wmsStorageMapper.insertWmsStorage(wmsStorage);
    }

    /**
     * 修改入库记录
     * 
     * @param wmsStorage 入库记录
     * @return 结果
     */
    @Override
    public int updateWmsStorage(WmsStorage wmsStorage)
    {
        return wmsStorageMapper.updateWmsStorage(wmsStorage);
    }

    /**
     * 删除入库记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWmsStorageByIds(String ids)
    {
        return wmsStorageMapper.deleteWmsStorageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库记录信息
     * 
     * @param stoId 入库记录ID
     * @return 结果
     */
    @Override
    public int deleteWmsStorageById(Long stoId)
    {
        return wmsStorageMapper.deleteWmsStorageById(stoId);
    }


    /**
     * 根据订货单id完成入库
     * @param purId
     * @return
     */
    @Override
    public int insertPro(Integer purId) {
        //查询订货单信息
        WmsPurchase wmsPurchase=wmsPurchaseMapper.selectWmsPurchaseById(purId);
        System.out.println(wmsPurchase);
        //添加入库记录数据
        WmsStorage wmsStorage=new WmsStorage();
        wmsStorage.setStoPurId(wmsPurchase.getPurId());
        wmsStorage.setStoPerson("admin");
        wmsStorage.setStoTime(new Date());
        int i=wmsStorageMapper.insertWmsStorage(wmsStorage);

        //修改订货单信息
        wmsPurchase.setPurStoId(wmsStorage.getStoId());
        wmsPurchase.setPurStatus("入库完成");
        i +=wmsPurchaseMapper.updateWmsPurchase(wmsPurchase);

        //查询该订单下的所有商品，修改库存
        WmsPurchaseDetails wmsPurchaseDetails =new WmsPurchaseDetails();
        wmsPurchaseDetails.setDetPurId(purId);
        List<WmsPurchaseDetails> wmsPurchaseDetailsList =wmsPurchaseDetailsMapper.selectWmsPurchaseDetailsList(wmsPurchaseDetails);
        for (WmsPurchaseDetails wd : wmsPurchaseDetailsList) {
            WmsStroeSpace wmsStroeSpace = wmsStroeSpaceMapper.selectWmsStroeSpaceByPId(wd.getDetPurPId());
            wmsStroeSpace.setStroeSpaceNum(wmsStroeSpace.getStroeSpaceNum()+wd.getDetPurPCount());
            i+=wmsStroeSpaceMapper.updateWmsStroeSpaceByPId(wmsStroeSpace);
        }

        return i;
    }
}
